home *** CD-ROM | disk | FTP | other *** search
- /*
- File: DragTxPv.cpp
-
- Contains: Static functions used by DragText implementation.
-
- Owned by: Craig Carper
-
- Copyright: © 1993 - 1995 by Apple Computer, Inc., all rights reserved.
-
- Change History (most recent first):
-
- <17> 9/13/95 DM 1277216 GM:API return no ODPoints nor
- ODPolygons
- <16> 9/4/95 TJ Added Includes to Compile with out
- PC-Headers.
- <15> 6/28/95 CC 1262618: Added GetFSSpecFromHFSProperty(),
- ReadTextFile(), InsertFromTextFile(),
- SURemoveValue(), and IsFrontProcess().
- <14> 6/26/95 JBS &DMc&TÇ 1242642 BB: Refcounting Fixes.
- <13> 6/6/95 CC 1256250: DrawLinkBorder: Clip out
- surrounding frame when drawing link
- borders.
- <12> 6/5/95 TJ Moved AltPoint up above where ODTypes is
- included.
- <11> 5/31/95 TJ Included GXMath.h insted of math routines.h
- <10> 5/26/95 RR #1251403: Multithreading naming support
- <9> 5/25/95 jpa Use new control/dialog mgr consts [1253324]
- <8> 2/24/95 jpa Use ODNewRgn. [1220810]
- <7> 2/24/95 CC Disabled link border debugging strings.
- <6> 1/12/95 jpa Don't use obsolete Toolbox names [1211211]
- <5> 1/9/95 NP 1194880: SemtIntf name changes.
- <4> 12/20/94 VL 1195012: Make Storage calls be
- marshallable.
- <3> 11/28/94 CC Checkin of changes to revision 2 noted
- below.
- <2+1> 11/14/94 CC Added DrawLinkBorder, LineHeightAtOffset,
- TELastLineOffset, DragTextDialogFilter.
- TEGetLine returns nLines-1 if last offset
- (or greater) is input.
- <2> 9/30/94 CC 1189416 - Utility functions for DragText
- 'ternalization.
- <1> 9/29/94 RA first checked in
- <1> 9/8/94 CC first checked in
-
- In Progress:
-
- */
-
- #ifndef _ALTPOINT_
- #include "AltPoint.h" // Use C++ savvy ODPoint and ODRect
- #endif
-
- #include <DragText.xh>
- #include "DragTxPv.h"
-
- #ifndef _DRAGDEF_
- #include <DragDef.h>
- #endif
-
- #ifndef _DRAGCONS_
- #include <DragCons.h>
- #endif
-
- #ifndef _PLFMDEF_
- #include "PlfmDef.h"
- #endif
-
- #ifndef __QUICKDRAW__
- #include <Quickdraw.h>
- #endif
-
- #ifndef __STRING__
- #include <String.h>
- #endif
-
- #ifndef __OSUTILS__
- #include <OSUtils.h> // SysBeep, GetDateTime, etc.
- #endif
-
- #ifndef __OSEVENTS__
- #include <OSEvents.h>
- #endif
-
- #ifndef __TOOLUTILS__
- #include <ToolUtils.h> // HiWord etc.
- #endif
-
- #ifndef __SCRAP__
- #include <Scrap.h>
- #endif
-
- #ifndef __LOWMEM__
- #include <LowMem.h>
- #endif
-
- #ifndef __FILES__
- #include <Files.h>
- #endif
-
- #ifdef __SC__
-
- #ifndef __LANGUAGE__
- #include <Language.h>
- #endif
-
- #endif // __SC__
-
- #ifndef SOM_Module_OpenDoc_StdProps_defined
- #include <StdProps.xh>
- #endif
-
- #ifndef SOM_Module_OpenDoc_StdTypes_defined
- #include <StdTypes.xh>
- #endif
-
- #ifndef SOM_ODStorageUnit_xh
- #include <StorageU.xh>
- #endif
-
- #ifndef SOM_ODDraft_xh
- #include <Draft.xh>
- #endif
-
- #ifndef SOM_ODDocument_xh
- #include <Document.xh>
- #endif
-
- #ifndef SOM_ODContainer_xh
- #include <ODCtr.xh>
- #endif
-
- #ifndef _ODDEBUG_
- #include "ODDebug.h"
- #endif
-
- #ifndef SOM_ODSession_xh
- #include <ODSessn.xh>
- #endif
-
- #ifndef SOM_ODWindowState_xh
- #include <WinStat.xh>
- #endif
-
- #ifndef SOM_ODArbitrator_xh
- #include <Arbitrat.xh>
- #endif
-
- #ifndef SOM_ODDispatcher_xh
- #include <Disptch.xh>
- #endif
-
- #ifndef SOM_ODFrame_xh
- #include <Frame.xh>
- #endif
-
- #ifndef SOM_ODFacet_xh
- #include <Facet.xh>
- #endif
-
- #ifndef SOM_ODFrameFacetIterator_xh
- #include <FrFaItr.xh>
- #endif
-
- #ifndef SOM_ODCanvas_xh
- #include <Canvas.xh>
- #endif
-
- #ifndef SOM_ODDraft_xh
- #include <Draft.xh>
- #endif
-
- #ifndef SOM_ODShape_xh
- #include <Shape.xh>
- #endif
-
- #ifndef SOM_ODTransform_xh
- #include <Trnsform.xh>
- #endif
-
- #ifndef SOM_ODWindow_xh
- #include <Window.xh>
- #endif
-
- #ifndef SOM_ODMenuBar_xh
- #include <MenuBar.xh>
- #endif
-
- #ifndef _EXCEPT_
- #include "Except.h"
- #endif
-
- #ifndef SOM_ODDragAndDrop_xh
- #include <DragDrp.xh>
- #endif
-
- #ifndef SOM_ODDragItemIterator_xh
- #include <DgItmIt.xh>
- #endif
-
- #ifndef SOM_ODStorageUnitView_xh
- #include <SUView.xh>
- #endif
-
- #ifndef SOM_Module_OpenDoc_Commands_defined
- #include <CmdDefs.xh>
- #endif
-
- #ifndef SOM_ODUndo_xh
- #include <Undo.xh>
- #endif
-
- #ifndef SOM_ODFocusSet_xh
- #include <FocusSet.xh>
- #endif
-
- #ifndef SOM_Module_OpenDoc_Foci_defined
- #include <Foci.xh>
- #endif
-
- #ifndef _ODUTILS_
- #include <ODUtils.h>
- #endif
-
- #ifndef _ORDCOLL_
- #include "OrdColl.h"
- #endif
-
- #ifndef SOM_ODClipboard_xh
- #include <Clipbd.xh>
- #endif
-
- #ifndef SOM_ODLink_xh
- #include <Link.xh>
- #endif
-
- #ifndef SOM_ODLinkSource_xh
- #include <LinkSrc.xh>
- #endif
-
- #ifndef SOM_ODLinkSpec_xh
- #include <LinkSpec.xh>
- #endif
-
- #ifndef _ODMEMORY_
- #include "ODMemory.h"
- #endif
-
- #ifndef _ISOSTR_
- #include "ISOStr.h"
- #endif
-
- #ifndef SOM_ODNameSpaceManager_xh
- #include <NmSpcMg.xh>
- #endif
-
- #ifndef SOM_ODValueNameSpace_xh
- #include <ValueNS.xh>
- #endif
-
- #ifndef SOM_Module_OpenDoc_StdDefs_defined
- #include <StdDefs.xh> //jpa
- #endif
-
- #ifndef _FOCUSLIB_
- #include "FocusLib.h" // for drawing/printing
- #endif
-
- #ifndef __STDTYPIO__
- #include "StdTypIO.h" // for labs
- #endif
-
- #ifndef __GXMATH__
- #include <GXMath.h>
- #endif
-
- #ifndef __QDOFFSCREEN__
- #include "QDOffscreen.h" // for GWorldPtr, StScrpHandle
- #endif
-
- #ifndef __STDLIB__
- #include "stdlib.h" // for labs
- #endif
-
- #ifndef _PASCLSTR_
- #include "PasclStr.h"
- #endif
-
- #ifndef _STORUTIL_
- #include <StorUtil.h>
- #endif
-
- #ifndef _TEMPOBJ_
- #include "TempObj.h"
- #endif
-
- #pragma segment DragTextPrivate
-
- #ifdef ODDebug
- #define DEBUG_DRAGTEXT 1
- #else
- #undef DEBUG_DRAGTEXT
- #endif
-
- #undef DEBUG_LINK_BORDERS
-
- //==============================================================================
- // Forward Declarations
- //==============================================================================
-
- ODSShort LineHeightAtOffset(ODSShort offset, TEHandle theTEHandle);
- short TENextLineOffset(short line, TEHandle theTE);
-
- //-------------------------------------------------------------------------
- // IncludedInRange
- //-------------------------------------------------------------------------
-
- extern ODBoolean IncludedInRange(ODSShort start, ODSShort end, ODSShort startRange, ODSShort endRange)
- {
- return (startRange <= start) && (endRange >= end);
- }
-
- //-------------------------------------------------------------------------
- // OneDragItem
- //-------------------------------------------------------------------------
-
- extern ODBoolean OneDragItem(Environment *ev, ODDragItemIterator* dropInfo)
- {
- ODBoolean result = kODFalse;
-
- dropInfo->First(ev);
- result = dropInfo->IsNotComplete(ev);
- if (result)
- {
- dropInfo->Next(ev);
- result = !dropInfo->IsNotComplete(ev);
- }
-
- return result;
- }
-
-
- //-------------------------------------------------------------------------
- // IsKind
- //-------------------------------------------------------------------------
-
- extern ODBoolean IsKind(ODType type, ODISOStr kind)
- {
- return !ODISOStrCompare( (ODISOStr) type, kind );
- }
-
- //-------------------------------------------------------------------------
- // PeekIntoValue
- //-------------------------------------------------------------------------
-
- extern char PeekIntoValue(Environment *ev, ODStorageUnit* su, ODValueType kind, ODSLong offset)
- // Returns the character at the specified offset; negative offsets are relative to the
- // end, with -1 denoting the last character.
- {
- ODStorageUnit* suRelease = (ODStorageUnit*) kODNULL;
- char ch = ' ';
- TRY
- if ( IsKind(kind, kODKindTestDrag) )
- {
- ODStorageUnitRef aSURef;
- su->Focus(ev, kODPropContents, kODPosUndefined, (ODValueType) kODKindTestDrag, 0, kODPosUndefined);
- StorageUnitGetValue(su, ev, sizeof(ODStorageUnitRef),&aSURef);
- suRelease = su->GetDraft(ev)->AcquireStorageUnit(ev, su->GetIDFromStorageUnitRef(ev, aSURef));
- su = suRelease;
- su->Focus(ev, kDragTextPropText, kODPosUndefined, (ODValueType) kODAppleTEXT, 0, kODPosUndefined);
- }
- else
- {
- su->Focus(ev, kODPropContents, kODPosUndefined, kind, 0, kODPosUndefined);
- }
-
- ODULong size = su->GetSize(ev);
- if ( offset < 0 )
- {
- // Rather than mix signed and unsigned values in expressions,
- // convert to an unsigned offset relative to the end.
- ODULong endOffset = labs(offset);
- if ( (size >= endOffset) )
- {
- su->SetOffset(ev, size - endOffset);
- StorageUnitGetValue(su, ev, 1, (ODValue) &ch);
- }
- }
- else
- {
- if ( offset < size )
- {
- su->SetOffset(ev, offset);
- StorageUnitGetValue(su, ev, 1, (ODValue) &ch);
- }
- }
- su->SetOffset(ev, 0);
- CATCH_ALL
- ENDTRY
-
- ODReleaseObject(ev,suRelease);
-
- return ch;
- }
-
- //-------------------------------------------------------------------------
- // TempSelect
- //-------------------------------------------------------------------------
-
- extern void TempSelect(ODSShort start, ODSShort end, TEHandle theTE, ODBoolean isActive)
- {
- // Temporarily change the selection to the argument range.
- // The hilighting is not changed.
-
- RgnHandle saveClip;
-
- if (isActive)
- {
- saveClip = ODNewRgn();
- GetClip(saveClip);
-
- RgnHandle clipRgn;
- clipRgn = ODNewRgn();
- SetClip(clipRgn);
- DisposeRgn(clipRgn);
- }
-
- TESetSelect(start, end, theTE);
-
- if (isActive)
- {
- SetClip(saveClip);
- DisposeRgn(saveClip);
- }
- }
-
-
- //-------------------------------------------------------------------------
- // RangesOverlap
- //-------------------------------------------------------------------------
-
- extern ODBoolean RangesOverlap(ODSShort start1, ODSShort end1,ODSShort start2,ODSShort end2)
- // Returns kODTrue if the two argument text offset ranges overlap
- {
- if (start1 >= end2)
- return kODFalse;
- else if (start2 >= end1)
- return kODFalse;
- else if (start1 < start2) {
- if (end1 <= start2)
- return kODFalse;
- else
- return kODTrue;
- }
- else
- return kODTrue;
- }
-
- //-------------------------------------------------------------------------
- // PasteThisKind
- //-------------------------------------------------------------------------
-
- extern ODBoolean PasteThisKind(Environment *ev, ODStorageUnit* su, ODType aKind, ODType specifiedKind)
- // Returns true if aKind exists in the contents property of the argument storage unit
- // and specifiedKind is either nil (don't care) or the same as aKind.
- {
- return ( su->Exists(ev, kODPropContents, aKind, kODPosAll)
- && ((specifiedKind == (ODType) kODNULL) || (IsKind(aKind, specifiedKind))) );
- }
-
- //-------------------------------------------------------------------------
- // LinkSpecInSU
- //-------------------------------------------------------------------------
-
- extern ODBoolean LinkSpecInSU(Environment *ev, ODStorageUnit* su)
- {
- ODBoolean result = kODFalse;
-
- TRY
- result = su->Exists(ev, kODPropLinkSpec, (ODValueType) kODLinkSpec, 0);
- CATCH_ALL
- ENDTRY
-
- return result;
- }
-
- //-------------------------------------------------------------------------
- // ValueOnClipboard
- //-------------------------------------------------------------------------
-
- extern ODBoolean ValueOnClipboard(Environment *ev, ODValueType type, ODSession* session)
- {
- ODBoolean result = kODFalse;
- ODClipboard* clipboard = session->GetClipboard(ev);
-
- TRY
- ODStorageUnit* su = clipboard->GetContentStorageUnit(ev);
- result = su->Exists(ev, kODPropContents, type, 0);
- CATCH_ALL
- ENDTRY
-
- return result;
- }
-
- //-------------------------------------------------------------------------
- // GetAggregateTranslation
- //-------------------------------------------------------------------------
-
- extern ODPoint GetAggregateTranslation(Environment *ev, ODFacet* facet)
- {
- ODPoint xForm(0,0);
- TempODTransform facetXform = facet->AcquireWindowContentTransform(ev, kODNULL);
- facetXform->TransformPoint(ev, &xForm);
- return xForm;
- }
-
- //-------------------------------------------------------------------------
- // CheckMarkMenu
- //-------------------------------------------------------------------------
-
- extern void CheckMarkMenu(MenuHandle menu, Str255 checkItem, ODBoolean checkIt)
- {
- ODSShort numMenuItems = CountMItems( menu );
- Str255 menuItemString;
-
- for (ODSShort menuItemNum = 1; menuItemNum <= numMenuItems; menuItemNum++)
- {
- GetMenuItemText( menu, menuItemNum, menuItemString );
-
- if ( IUEqualString(checkItem, menuItemString) == 0 )
- {
- if ( checkIt )
- CheckItem( menu, menuItemNum, kODTrue );
- else
- CheckItem( menu, menuItemNum, kODFalse );
- }
- }
- }
-
- //-------------------------------------------------------------------------
- // DrawOffscreen
- //-------------------------------------------------------------------------
-
- extern WindowOffscreen *DrawOffscreen(WindowPtr theWindow)
- {
- WindowOffscreen *theOffscreen;
- GWorldPtr theWorld;
- Rect globalRect;
-
- if ((theOffscreen = (WindowOffscreen *) ODNewPtr(sizeof(WindowOffscreen), kDefaultHeapID)) == 0L)
- return(0L);
-
- SetPort(theWindow);
- GetGWorld(&theOffscreen->windowPort, &theOffscreen->windowDevice);
-
- globalRect = theWindow->portRect;
- LocalToGlobal((Point *) &globalRect.top);
- LocalToGlobal((Point *) &globalRect.bottom);
-
- if (NewGWorld(&theWorld, 0, &globalRect, 0L, 0L, 0) == noErr)
- {
- SetGWorld(theWorld, 0L);
- if (!LockPixels(theWorld->portPixMap))
- {
- DisposeGWorld(theWorld);
- ODDisposePtr((Ptr)theOffscreen);
- return(0L);
- }
-
- CopyBits(&theWindow->portBits,
- &((GrafPtr) theWorld)->portBits,
- &theWindow->portRect,
- &theWorld->portRect,
- srcCopy, 0L);
-
- theOffscreen->offscreenWorld = theWorld;
- SetPort(theWindow);
- return(theOffscreen);
- }
- else
- {
- ODDisposePtr((Ptr)theOffscreen);
- return(0L);
- }
- }
-
- //-------------------------------------------------------------------------
- // DrawOnscreen
- //-------------------------------------------------------------------------
-
- extern void DrawOnscreen(WindowOffscreen *theOffscreen)
- {
- SetGWorld(theOffscreen->windowPort, theOffscreen->windowDevice);
- CopyBits(&((GrafPtr) theOffscreen->offscreenWorld)->portBits,
- &((GrafPtr) theOffscreen->windowPort)->portBits,
- &theOffscreen->offscreenWorld->portRect,
- &theOffscreen->windowPort->portRect,
- srcCopy, 0L);
- UnlockPixels(theOffscreen->offscreenWorld->portPixMap);
- DisposeGWorld(theOffscreen->offscreenWorld);
- ODDisposePtr((Ptr) theOffscreen);
- }
-
- //-------------------------------------------------------------------------
- // DrawCaret
- //-------------------------------------------------------------------------
- /*
- * DrawCaret draws a caret in a TextEdit field at the given offset. DrawCaret
- * expects the port to be set to the port that the TextEdit field is in.
- * A clipping region may be optionally given to clip the drawing to. DrawCaret
- * inverts the image of the caret onto the screen.
- */
-
- extern void DrawCaret(short offset, TEHandle theTE, RgnHandle clipRgn)
- {
- Point theLoc;
- short theLine, lineHeight;
- RgnHandle oldClip;
-
- /*
- * Get the coordinates and the line of the offset to draw the caret.
- */
-
- theLoc = TEGetPoint(offset, theTE);
- theLine = TEGetLine(offset, theTE);
-
- /*
- * For some reason, TextEdit dosen't return the proper coordinates
- * of the last offset in the field if the last character in the record
- * is a carriage return. TEGetPoint returns a point that is one line
- * higher than expected. The following code fixes this problem.
- */
-
- if ((offset == (**theTE).teLength) &&
- (*((char **)(**theTE).hText))[(**theTE).teLength - 1] == 0x0D) {
- theLoc.v += (short)(TEGetHeight(theLine, theLine, theTE));
- }
-
- /*
- * Always invert the caret when drawing.
- */
-
- PenMode(patXor);
-
- /*
- * Get the height of the line that the offset points to.
- */
-
- lineHeight = (short)(TEGetHeight(theLine, theLine, theTE));
-
- /*
- * If we are given a clip region, use it to draw the caret.
- */
-
- if (clipRgn) {
- oldClip = ODNewRgn();
- GetClip(oldClip);
- SetClip(clipRgn);
- }
-
- /*
- * Draw the appropriate caret image.
- */
-
- MoveTo(theLoc.h - 1, theLoc.v - 1);
- Line(0, 1 - lineHeight);
-
- /*
- * Restore the clipping region, if necessary.
- */
-
- if (clipRgn) {
- SetClip(oldClip);
- DisposeRgn(oldClip);
- }
-
- PenNormal();
- }
-
- //-------------------------------------------------------------------------
- // GetCharAtOffset
- //-------------------------------------------------------------------------
-
- extern char GetCharAtOffset(short offset, TEHandle theTE)
- {
- if (offset < 0)
- return(0x0D);
-
- return(((char *) *((**theTE).hText))[offset]);
- }
-
- //-------------------------------------------------------------------------
- // WhiteSpace
- //-------------------------------------------------------------------------
-
- extern Boolean WhiteSpace(char theChar)
- {
- return((theChar == ' ') || (theChar == 0x0D));
- }
-
- //-------------------------------------------------------------------------
- // WhiteSpaceAtOffset
- //-------------------------------------------------------------------------
-
- extern Boolean WhiteSpaceAtOffset(short offset, TEHandle theTE)
- {
- char theChar;
-
- if (offset < 0)
- return(true);
-
- theChar = ((char *) *((**theTE).hText))[offset];
- return((theChar == ' ') || (theChar == 0x0D));
- }
-
-
- //-------------------------------------------------------------------------
- // InsertTextAtOffset
- //-------------------------------------------------------------------------
-
- extern void InsertTextAtOffset(short offset, char *theBuf, long size, StScrpHandle theStyl, TEHandle theTE)
- {
- if (size == 0)
- return;
-
- //
- // If inserting at the end of a word and the selection does not begin with
- // a space, insert a space before the insertion.
- //
-
- if (!WhiteSpaceAtOffset(offset - 1, theTE) &&
- WhiteSpaceAtOffset(offset, theTE) &&
- !WhiteSpace(theBuf[0])) {
-
- TESetSelect(offset, offset, theTE);
- TEKey(' ', theTE);
- offset++;
- }
-
- //
- // If inserting at the beginning of a word and the selection does not end
- // with a space, insert a space after the insertion.
- //
-
- if ( WhiteSpaceAtOffset(offset - 1, theTE) &&
- !WhiteSpaceAtOffset(offset, theTE) &&
- !WhiteSpace(theBuf[size - 1])) {
-
- TESetSelect(offset, offset, theTE);
- TEKey(' ', theTE);
- }
-
- TESetSelect(offset, offset, theTE);
- TEStyleInsert(theBuf, size, theStyl, theTE);
- TESetSelect(offset, offset + size, theTE);
- }
-
- //-------------------------------------------------------------------------
- // TEIsFrontOfLine
- //-------------------------------------------------------------------------
- /*
- * TEIsFrontOfLine, given an offset and a TextEdit handle, returns true if
- * the given offset is at the beginning of a line start.
- */
-
- extern short TEIsFrontOfLine(short offset, TEHandle theTE)
-
- { short line = 0;
-
- if ((**theTE).teLength == 0)
- return(true);
-
- if (offset >= (**theTE).teLength)
- return( (*((char **)(**theTE).hText))[(**theTE).teLength - 1] == 0x0d );
-
- while ((**theTE).lineStarts[line] < offset)
- line++;
-
- return( (**theTE).lineStarts[line] == offset );
- }
-
- //-------------------------------------------------------------------------
- // TEGetLine
- //-------------------------------------------------------------------------
- /*
- * TEGetLine, given an offset and a TextEdit handle, returns the line number
- * of the line that contains the offset.
- */
-
- extern short TEGetLine(short offset, TEHandle theTE)
-
- { short line = 0;
-
- if (offset >= (**theTE).teLength)
- line = (**theTE).nLines-1;
- else
- while ((**theTE).lineStarts[line+1] <= offset)
- line++;
-
- return(line);
- }
-
- //-------------------------------------------------------------------------
- // TENextLineOffset
- //-------------------------------------------------------------------------
- /*
- * TENextLineOffset, given a line and a TextEdit handle, returns the first
- * offset of the next line.
- */
-
- short TENextLineOffset(short line, TEHandle theTE)
-
- { short offset;
-
- if ( line >= (**theTE).nLines )
- offset = (**theTE).teLength;
- else
- offset = (**theTE).lineStarts[line+1];
-
- return offset;
- }
-
- //-------------------------------------------------------------------------
- // AdjustDocumentView
- //-------------------------------------------------------------------------
- /*
- * AdjustDocumentView scrolls the document image by the appropriate amount
- * if the scroll bar value has changed.
- */
-
- extern void AdjustDocumentView(Document *theDocument)
- {
- short delta, docTop, docTopLimit;
-
- delta = (theDocument->vScrollPos - GetControlValue(theDocument->vScroll)) * ScrollResolution;
-
- if (delta) {
- if (delta > 0) {
- docTop = (**(theDocument->theTE)).destRect.top;
- docTopLimit = (**(theDocument->theTE)).viewRect.top + TopMargin;
- if (docTop + delta > docTopLimit) {
- delta = docTopLimit - docTop;
- }
- }
- TEScroll(0, delta, theDocument->theTE);
- theDocument->vScrollPos = GetControlValue(theDocument->vScroll);
- }
- }
-
- //-------------------------------------------------------------------------
- // ScrollProc
- //-------------------------------------------------------------------------
- /*
- * ScrollProc is the function used in a call to TrackControl for
- * scrolling a document window.
- */
-
- extern pascal void ScrollProc(ControlHandle theControl, short theCode)
- {
- short delta = 0, pageDelta;
- Document *theDocument;
- Rect viewRect;
-
- if (theCode == 0)
- return;
-
- theDocument = (Document *) (**theControl).contrlRfCon;
- viewRect = (**(theDocument->theTE)).viewRect;
- pageDelta = ((viewRect.bottom - viewRect.top) / ScrollResolution) - 1;
-
- switch(theCode) {
- case kControlUpButtonPart:
- delta = -1;
- break;
- case kControlDownButtonPart:
- delta = 1;
- break;
- case kControlPageUpPart:
- delta = -pageDelta;
- break;
- case kControlPageDownPart:
- delta = pageDelta;
- break;
- }
-
- SetControlValue(theControl, GetControlValue(theControl) + delta);
- AdjustDocumentView((Document *) (**theControl).contrlRfCon);
- }
-
- //-------------------------------------------------------------------------
- // SUExistsThenFocus
- //-------------------------------------------------------------------------
- ODBoolean SUExistsThenFocus(Environment* ev, ODStorageUnit* su, ODPropertyName prop, ODValueType type)
- {
- return ODSUExistsThenFocus(ev, su, prop, type);
- }
-
- //-------------------------------------------------------------------------
- // SUForceFocus
- //-------------------------------------------------------------------------
- void SUForceFocus(Environment* ev, ODStorageUnit* su, ODPropertyName prop, ODValueType type)
- {
- ODSUForceFocus(ev, su, prop, type);
- }
-
- //-------------------------------------------------------------------------
- // SUSetValue
- //-------------------------------------------------------------------------
- void SUSetValue(Environment* ev, ODStorageUnit* su, ODValue value, ODULong size)
- {
- ODULong oldSize = su->GetSize(ev);
- StorageUnitSetValue(su, ev, size, value);
- if (oldSize > size)
- su->DeleteValue(ev, oldSize - size);
- }
-
- //-------------------------------------------------------------------------
- // SUFocusAndSetValue
- //-------------------------------------------------------------------------
- void SUFocusAndSetValue(Environment* ev,
- ODStorageUnit* su,
- ODPropertyName prop,
- ODValueType type,
- ODValue value,
- ODULong size)
- {
- SUForceFocus(ev, su, prop, type);
- SUSetValue(ev, su, value, size);
- }
-
- //-------------------------------------------------------------------------
- // SURemoveProperty
- //-------------------------------------------------------------------------
- void SURemoveProperty(Environment* ev, ODStorageUnit* su, ODPropertyName prop)
- {
- if ( su->Exists(ev, prop, kODNULL, 0) )
- {
- su->Focus(ev, prop, kODPosUndefined, kODNULL, (ODValueIndex)0, kODPosUndefined);
- su->Remove(ev);
- }
- }
-
- //-------------------------------------------------------------------------
- // SURemoveValue
- //-------------------------------------------------------------------------
- void SURemoveValue(Environment* ev, ODStorageUnit* su, ODPropertyName prop, ODValueType type)
- {
- if ( su->Exists(ev, prop, type, 0) )
- {
- su->Focus(ev, prop, kODPosUndefined, type, (ODValueIndex)0, kODPosUndefined);
- su->Remove(ev);
- }
- }
-
- //-------------------------------------------------------------------------
- // LineHeightAtOffset
- //-------------------------------------------------------------------------
- ODSShort LineHeightAtOffset(ODSShort offset, TEHandle theTEHandle)
- {
- ODSShort height;
-
- // $$$$$ It seems that (*theTEHandle)->lineHeight is greater than zero
- // even if the line height table exists, so just assume the lineHeigthTable
- // exists.
-
- //if ( (*theTEHandle)->lineHeight > 0 )
- //{
- // height = (*theTEHandle)->lineHeight; /* top o' the line to you! */
- //}
- //else
- {
- ODSShort line = TEGetLine (offset, theTEHandle);
- height = TEGetHeight(line, line, theTEHandle);
- }
-
- return height;
- }
-
- //-------------------------------------------------------------------------
- // LineBorderFromRange
- //-------------------------------------------------------------------------
- RgnHandle LineBorderFromRange(ODSShort* startOffset, ODSShort endOffset, TEHandle theTEHandle)
- {
- if (*startOffset >= endOffset)
- return kODNULL;
-
- Point startPoint;
- Point endPoint;
-
- RgnHandle borderRgn = ODNewRgn();
- OpenRgn();
-
- ODSShort startLine = TEGetLine (*startOffset, theTEHandle);
- ODSShort endLine = TEGetLine (endOffset, theTEHandle);
-
- #ifdef DEBUG_LINK_BORDERS
- somPrintf("DragText: LineBorderFromRange: start line(offset) = %d(%d), end line(offset) = %d(%d)\n",
- startLine, *startOffset, endLine, endOffset);
- #endif
-
- startPoint = TEGetPoint((short)*startOffset, theTEHandle);
-
- if ( (endLine == startLine) )
- {
- endPoint = TEGetPoint((short)endOffset, theTEHandle);
- }
- else
- {
- endPoint.h = (*theTEHandle)->viewRect.right;
- endPoint.v = startPoint.v;
- }
-
- endPoint.v -= LineHeightAtOffset(*startOffset, theTEHandle);
-
- #ifdef DEBUG_LINK_BORDERS
- somPrintf("DragText: LineBorderFromRange: Line border start(h, v) = (%d, %d), end(h, v) = (%d, %d)\n",
- startPoint.h, startPoint.v, endPoint.h, endPoint.v);
- #endif
-
- MoveTo(startPoint.h, startPoint.v);
- LineTo(endPoint.h, startPoint.v);
- LineTo(endPoint.h, endPoint.v);
- LineTo(startPoint.h, endPoint.v);
- LineTo(startPoint.h, startPoint.v);
-
- CloseRgn(borderRgn);
-
- *startOffset = TENextLineOffset(startLine, theTEHandle);
-
- return borderRgn;
- }
-
- //-------------------------------------------------------------------------
- // DrawLinkBorder
- //-------------------------------------------------------------------------
- //
- // Based on C.K. Haun's BorderTextSection
- /* this borders the selected section. It's a pain. */
- /* BUT, it's a pain because I'm using TextEdit. If you're writing a word processor */
- /* or another application which does it's own text processing, this will be a lot easier */
- /* •••• NOTE: this doesn't look very good when a subscriber is embedded in a publisher, since */
- /* the lines of course overlap. I don't know how to deal with this yet. */
- extern void DrawLinkBorder(Environment *ev,
- ODFacet* facet,
- ODSShort linkStart,
- ODSShort linkEnd,
- TEHandle theTEHandle,
- LinkBorderStyle borderStyle)
- {
- RgnHandle borderRgn = ODNewRgn();
-
- ODPoint xForm;
- ODShape* clipShape = kODNULL;
- ODShape* frameShape = kODNULL;
- RgnHandle clipRgn, saveClip, frameRgn;
- Rect bounds;
- GrafPtr savedQDPort;
-
- // $$$$$ KLUDGE: When linked content is moved, its possible for linkStart to be beyond the
- // current length of the text. In that case just return.
-
- if ( linkStart >= (**theTEHandle).teLength )
- return;
-
- if ( linkEnd > (**theTEHandle).teLength )
- linkEnd = (**theTEHandle).teLength;
-
- // Link Border patterns
- Pattern unselectedLinkBorder = {0xCC, 0x99, 0x33, 0x66, 0xCC, 0x99, 0x33, 0x66};
- //Pattern oldUnselectedLinkBorder = {0x33, 0xCC, 0x33, 0xCC, 0x33, 0xCC, 0x33, 0xCC};
- //Pattern davesLinkBorder = {0x88, 0x55, 0x22, 0x55, 0x88, 0x55, 0x22, 0x55};
- Pattern selectedLinkBorder = {0x77, 0xEE, 0xDD, 0xBB, 0x77, 0xEE, 0xDD, 0xBB};
- //Pattern oldSelectedLinkBorder = {0x77, 0xEE, 0x77, 0xEE, 0x77, 0xEE, 0x77, 0xEE};
-
- ODSShort offsetStart = linkStart;
- do
- {
- RgnHandle lineRgn = LineBorderFromRange(&offsetStart, linkEnd, theTEHandle);
- if ( lineRgn != kODNULL )
- {
- UnionRgn(lineRgn, borderRgn, borderRgn);
- DisposeRgn(lineRgn);
- }
- #ifdef DEBUG_LINK_BORDERS
- somPrintf("DragText: DrawLinkBorder: Loop: start offset = %d, end offset = %d\n",
- offsetStart, linkEnd);
- #endif
- }
- while (offsetStart < linkEnd);
-
- #ifdef DEBUG_LINK_BORDERS
- somPrintf("DragText: DrawLinkBorder: Loop done!\n");
- #endif
-
- // Prepare to draw
- GetPort(&savedQDPort);
- SetPort(facet->GetCanvas(ev)->GetQDPort(ev));
- SetOrigin(0,0);
- xForm = GetAggregateTranslation(ev, facet);
- frameShape = facet->GetFrame(ev)->AcquireFrameShape(ev, kODNULL);
- frameRgn = frameShape->GetQDRegion(ev);
- clipShape = facet->AcquireClipShape(ev, kODNULL);
- clipRgn = clipShape->CopyQDRegion(ev);
- saveClip = ODNewRgn();
-
- GetClip(saveClip);
- bounds = (**frameRgn).rgnBBox;
- OffsetRect(&bounds, xForm.IntX(), xForm.IntY());
- OffsetRgn(clipRgn, xForm.IntX(), xForm.IntY());
- InsetRgn(clipRgn, 2, 2);
- SetClip(clipRgn);
-
- // Frame with four pixels of white
- PenSize(4, 4);
- PenPat(&ODQDGlobals.white);
- FrameRgn(borderRgn);
-
- if ( borderStyle == kLinkBorderOff )
- {
- Rect borderRect = (*borderRgn)->rgnBBox;
- TEUpdate(&borderRect, theTEHandle);
- }
- else
- {
- // Paint the border
- PenSize(2, 2);
- if ( borderStyle == kLinkBorderOn )
- PenPat(&unselectedLinkBorder);
- else
- PenPat(&selectedLinkBorder);
- InsetRgn(borderRgn, 1, 1);
- FrameRgn(borderRgn);
- }
-
- // Restore drawing environment
- SetPort(savedQDPort);
- SetClip(saveClip);
-
- if (frameShape) frameShape->Release(ev);
- if (clipShape) clipShape->Release(ev);
- DisposeRgn(saveClip);
- DisposeRgn(clipRgn);
-
- PenNormal();
-
- DisposeRgn(borderRgn);
- }
-
- //-------------------------------------------------------------------------
- // DragTextDialogFilter
- //-------------------------------------------------------------------------
-
- pascal Boolean DragTextDialogFilter(DialogPtr dialog,
- EventRecord* event,
- short* itemHit)
- {
- ODBoolean handled = kODFalse;
-
- Environment* ev = somGetGlobalEnvironment();
-
- ODSession* session = (ODSession*) ((WindowPeek)dialog)->refCon;
-
- if (event->what == nullEvent)
- {
- session->GetDispatcher(ev)->Dispatch(ev, (ODEventData*)event);
- }
- else if (event->what == updateEvt &&
- (WindowPtr) event->message != dialog)
- {
- session->GetDispatcher(ev)->Dispatch(ev, (ODEventData*)event);
- }
- else if (event->what == activateEvt &&
- (WindowPtr) event->message != dialog)
- {
- session->GetDispatcher(ev)->Dispatch(ev, (ODEventData*)event);
- }
- else if (event->what == mouseDown)
- {
- handled = kODFalse;
- }
- else if (event->what == mouseUp)
- {
- handled = kODTrue;
- }
- else if ((event->what == keyDown) || (event->what == autoKey))
- {
- handled = kODTrue;
- }
-
- return handled;
- }
-
- //-------------------------------------------------------------------------
- // GetFSSpecFromHFSProperty
- //-------------------------------------------------------------------------
-
- ODBoolean GetFSSpecFromHFSProperty(Environment* ev,
- ODStorageUnit* su,
- FSSpec* fileSpec)
- {
- ODBoolean result = kODFalse;
-
- if ( ODSUExistsThenFocus(ev, su, kODPropContents, kODApplehfs) )
- {
- ODULong hfsflavorSize = su->GetSize(ev);
- HFSFlavor* hfsflavorData = (HFSFlavor*) ODNewPtr(hfsflavorSize, kDefaultHeapID);
- StorageUnitGetValue(su, ev, hfsflavorSize, (ODValue) hfsflavorData);
-
- *fileSpec = hfsflavorData->fileSpec;
-
- ODDisposePtr((ODPtr) hfsflavorData);
- result = kODTrue;
- }
-
- return result;
- }
-
- //-------------------------------------------------------------------------
- // ReadTextFile
- //-------------------------------------------------------------------------
-
- ODHandle ReadTextFile(FSSpec* fileSpec, ODBoolean* truncated)
- {
- Size textSize;
- OSErr error = noErr;
- short fileRefNum = 0;
- ODHandle textHandle = kODNULL;
- Size bytesRead;
-
- *truncated = kODFalse;
-
- THROW_IF_ERROR(FSpOpenDF(fileSpec, fsRdPerm, &fileRefNum));
- TRY
- THROW_IF_ERROR(GetEOF(fileRefNum,&textSize));
-
- if ( textSize > kMaxTextSize )
- {
- *truncated = kODTrue;
- textSize = kMaxTextSize;
- }
-
- textHandle = ODNewHandle(textSize);
-
- if (textSize > 0)
- {
- THROW_IF_ERROR(SetFPos(fileRefNum,fsFromStart,0));
-
- bytesRead = textSize;
- ODLockHandle(textHandle);
- error = FSRead(fileRefNum, &bytesRead, *textHandle);
- ODUnlockHandle(textHandle);
- PRINT("DragText: ReadTextFile: Read %d bytes\n", bytesRead);
-
- if ( (error != noErr) && (error != eofErr) )
- THROW(error);
- }
-
- FSClose(fileRefNum);
- CATCH_ALL
- FSClose(fileRefNum);
- ODDisposeHandle(textHandle);
- RERAISE;
- ENDTRY
-
- return textHandle;
- }
-
- //-------------------------------------------------------------------------
- // InsertFromTextFile
- //-------------------------------------------------------------------------
-
- void InsertFromTextFile(FSSpec* fileSpec, TEHandle theTE, ODULong insertPosition)
- {
- Size textSize;
- OSErr error = noErr;
- short fileRefNum = 0;
- ODPtr textBuffer = kODNULL;
- Size bytesRead;
- Size totalBytesRead = 0;
-
- const ODULong kBufferSize = 2000;
-
- THROW_IF_ERROR(FSpOpenDF(fileSpec, fsRdPerm, &fileRefNum));
- TRY
- THROW_IF_ERROR(GetEOF(fileRefNum,&textSize));
-
- if (textSize == 0)
- {
- FSClose(fileRefNum);
- return;
- }
-
- if ( textSize + ((**theTE).teLength) > kMaxTextSize )
- THROW(kDragTextErrTooMuchText);
-
- THROW_IF_ERROR(SetFPos(fileRefNum,fsFromStart,0));
-
- textBuffer = ODNewPtr(kBufferSize);
-
- TESetSelect(insertPosition, insertPosition, theTE);
-
- bytesRead = kBufferSize;
- while ( error != noErr )
- {
- bytesRead = kBufferSize;
- error = FSRead(fileRefNum, &bytesRead, textBuffer);
- PRINT("DragText: InsertFromTextFile: Read %d bytes\n", bytesRead);
-
- TEInsert(textBuffer, bytesRead, theTE);
- PRINT("DragText: InsertFromTextFile: Text inserted\n");
-
- totalBytesRead += bytesRead;
- }
- PRINT("DragText: InsertFromTextFile: %d bytes inserted from file\n", totalBytesRead);
-
- ODDisposePtr(textBuffer);
-
- if ( error != eofErr )
- THROW(error);
-
- // The newly inserted text should begin on a new line
- if ( (insertPosition > 0) && (GetCharAtOffset(insertPosition, theTE) != 0x0D) )
- {
- PRINT("DragText: InsertFromTextFile: Inserting newline before text\n");
- TESetSelect(insertPosition, insertPosition, theTE);
- TEKey((char) 0x0D, theTE);
- }
-
- // Unless the file ended with an empty line, insert a new line at the end
- ODULong insertEnd = insertPosition + totalBytesRead;
- if ( (insertEnd < (**theTE).teLength) && (GetCharAtOffset(insertEnd, theTE) != 0x0D) )
- {
- PRINT("DragText: InsertFromTextFile: Inserting newline after text\n");
- TESetSelect(insertEnd+1, insertEnd+1, theTE);
- TEKey((char) 0x0D, theTE);
- }
-
- FSClose(fileRefNum);
- CATCH_ALL
- FSClose(fileRefNum);
- RERAISE;
- ENDTRY
- }
-
- //-------------------------------------------------------------------------
- // IsFrontProcess
- //-------------------------------------------------------------------------
-
- ODBoolean IsFrontProcess()
- {
- ProcessSerialNumber myProcess, frontProcess;
- ODBoolean inFront = kODFalse;
-
- GetCurrentProcess(&myProcess);
- GetFrontProcess(&frontProcess);
- SameProcess(&myProcess, &frontProcess, &inFront);
-
- return inFront;
- }
-